<?php
namespace app\wadmin\controller;
// +----------------------------------------------------------------------
// | bbcThink
// +----------------------------------------------------------------------
// | Copyright (c) 2025 http://bbcweb.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: bbcweb <22745529@qq.com>
// +----------------------------------------------------------------------
use think\facade\Db;
use app\BaseController;
use think\facade\View;
use app\service\MenuService;

//角色
class Role extends BaseController
{
    protected $table='role';
    public function index()
    {
        $p=input();
        $where=array();
        if($this->admin['id']>1)
        {
            $where[]=['danwei_id','=',$this->admin['danwei_id']];
        }
        if(!empty($p['keyword']))
        {
            $where[]=['name','like','%'.$p['keyword'].'%'];
        }
        $data_list = Db::name($this->table)->where($where)->select()->toArray();
        View::assign('data_list',$data_list);
        return View::fetch();
    }
    public function setauth()
    {
        $p=input();
        $where=array();
        $role_id=$p['id'];
        $role = Db::name($this->table)->where(['id'=>intval($p['id'])])->find();
        $where_menu=array();
        if($this->admin['id']>1)
        {
            $parent_role_id=explode(',',$this->admin['role_id']);
            $parent_menu_id=Db::name('menu')->alias('m')->join('RoleAuth ra','ra.menu_id=m.id')->where('ra.role_id','in',$parent_role_id)->column('menu_id');
            $where_menu[]=['id','in',$parent_menu_id];
            
        }
        $menu = Db::name('menu')->field('id,title')->where(['pid'=>0])->where($where_menu)->select()->toArray();
        foreach($menu as &$v)
        {
            $v['read']=Db::name('RoleAuth')->where(['menu_id'=>$v['id'],'role_id'=>$role_id])->count();
            $v['child'] = Db::name('menu')->field('id,title')->where(['pid'=>$v['id']])->where($where_menu)->select()->toArray();
            foreach($v['child'] as &$vv)            
            {
                $vv['read']=Db::name('RoleAuth')->where(['menu_id'=>$vv['id'],'role_id'=>$role_id])->count();
                $vv['child'] = Db::name('menu')->field('id,title')->where(['pid'=>$vv['id']])->select()->toArray();
                foreach($vv['child'] as &$vvv)
                {
                    $vvv['read']=Db::name('RoleAuth')->where(['menu_id'=>$vvv['id'],'role_id'=>$role_id])->count();
                }
            }
        }
        View::assign('role',$role);
        View::assign('menu',$menu);
        return View::fetch();
    }
    public function saveauth()
    {
        $p=input();
        // $res=Db::name('RoleAuth')->where(['role_id'=>$p['role_id'],'menu_id'=>$p['node_id']])->update(['read'=>$p['auth']]);
        // if(!$res)
        // {
        //     if($p['auth']==1)
        //     {
        //         $res = Db::name('RoleAuth')->insert(['role_id'=>$p['role_id'],'menu_id'=>$p['node_id'],'read'=>1]);
        //         return jsonRes($res);
        //     }
        // }
        Db::name('RoleAuth')->where(['role_id'=>$p['role_id']])->delete();
        foreach($p['id'] as $v)
        {
            $res = Db::name('RoleAuth')->insert(['role_id'=>$p['role_id'],'menu_id'=>$v,'read'=>1]);                   
        }
        return jsonRes(1);
    }
    public function modify()
    {
        $p=input();
        if($this->request->isPost())
        {
            if(empty($p['status']))
            {
                $p['status']=0;
            }
            if(empty($p['id']))
            {
                $p['danwei_id']=$this->admin['danwei_id'];
                unset($p['id']);
                $res = Db::name($this->table)->insert($p);
            }
            else{
            $res = Db::name($this->table)->where(['id'=>intval($p['id'])])->update($p);
            }
            return jsonReturn(1,'保存成功',$p);
        }
        $data = array();
        if(!empty($p['id']))
        {
            $data=Db::name($this->table)->where(['id'=>intval($p['id'])])->find();
        }
        View::assign('data',$data);
        return View::fetch();
    }
    public function status()
    {
        $p = input();
        $res = Db::name($this->table)->where(['id'=>intval($p['id'])])->update(['status'=>intval($p['status'])]);
        return jsonRes($res);
    }
    public function remove()
    {
        $p = input();       
        Db::name($this->table)->where(['id'=>$p['id']])->delete();
        return jsonRes(1);
    }
    public function removeall()
    {
        $p=input();
        foreach(explode(',',$p['id']) as $v)
        {
            Db::name($this->table)->where(['id'=>$v])->delete();
        }
        return jsonRes(1);
    }
}